package com.ly.mp.cmc.common.mapper.impl;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ly.mp.cmc.common.mapper.IbaseDal;
import org.hibernate.validator.internal.util.StringHelper;
import org.springframework.stereotype.Repository;

import java.beans.Transient;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ly-songpengfei
 * @Date: 2025/8/7 11:33
 * @Description: 通用持久层接口实现类
 * @Version: 1.0
 */
@Repository
public class BaseDal<T> implements IbaseDal<T> {

    // 具体类
    private Class<T> entityClass;
    // 操作DB工具
//    @Autowired
//    private JdbcTemplate jdbcTemplate;
    // 表名
    private String tableName = "";
    // 主键字段
    private String keyColumn = "";
    // 主键属性
    private String keyProp = "";
    // 是否批量操作
    private boolean currentOperationIsBatch = false;
    // 字段
    private List<Field> fieldList = new ArrayList();

    @SuppressWarnings("unchecked")
    public BaseDal() {
        Type superclass = this.getClass().getGenericSuperclass();
        ParameterizedType type = (ParameterizedType) superclass;
        entityClass = (Class<T>) type.getActualTypeArguments()[0];

        TableName tableName1 = entityClass.getAnnotation(TableName.class);
        if (tableName1 != null) {
            tableName = tableName1.value();
        }

        Field[] fields = entityClass.getDeclaredFields();
        for (int i = 0; fields != null && i < fields.length; i++) {
            Field temp = fields[i];
            TableId tableId = temp.getAnnotation(TableId.class);
            if (tableId != null) {
                keyColumn = tableId.value();
                keyProp = temp.getName();
                if (StringHelper.isNullOrEmptyString(keyColumn)) {
                    keyColumn = getTableColumn(keyProp);
                }
            }

            Transient trans = temp.getAnnotation(Transient.class);
            if (trans == null) {
                fieldList.add(temp);
            }
        }
        if (entityClass.getSuperclass() != null) {
            Field[] pFields = entityClass.getSuperclass().getDeclaredFields();
            for (int i = 0; pFields != null && i < pFields.length; i++) {
                Field pField = pFields[i];
                TableId tableId = pField.getAnnotation(TableId.class);
                if (tableId != null){
                    keyColumn = tableId.value();
                    keyProp = pField.getName();
                    if (StringHelper.isNullOrEmptyString(keyColumn)){
                        keyColumn = getTableColumn(keyProp);
                    }
                }

                Transient trans = pField.getAnnotation(Transient.class);
                if (trans == null){
                    fieldList.add(pField);
                }
            }
        }
    }

    private String getTableColumn(String keyProp) {
        if (StringHelper.isNullOrEmptyString(keyProp)) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < keyProp.length(); i++) {
                char c = keyProp.charAt(i);
                if ((c >= 'A') && (c <= 'Z')) {
                    sb.append("_").append((char) (c + 32));
                } else {
                    sb.append(c);
                }
            }
            return sb.toString();
        }
    }

    @Override
    public List<T> queryAll() {
        StringBuffer sql = new StringBuffer();
        sql.append(" select * from " + tableName);
        System.out.println(sql);
        for (Field field : fieldList) {
            System.out.println(field.getName());
        }
        System.out.println(keyColumn);
        System.out.println(keyProp);
        return new ArrayList();
//        jdbcTemplate.query(sql.toString(), );
    }

    @Override
    public T queryById(String id) {
        return null;
    }

    @Override
    public int save(T entity) throws IllegalAccessException, IllegalArgumentException {
        return 0;
    }

    @Override
    public int update(T entity) throws IllegalArgumentException, IllegalAccessException {
        return 0;
    }

    @Override
    public int update(T entity, String prop) throws IllegalArgumentException, IllegalAccessException {
        return 0;
    }

    @Override
    public int update(T entity, String[] props) throws IllegalArgumentException, IllegalAccessException {
        return 0;
    }

    @Override
    public int deleteById(String id) {
        return 0;
    }

    @Override
    public int deleteBatch(String[] ids) {
        return 0;
    }

    @Override
    public int enable(String[] ids) {
        return 0;
    }

    @Override
    public int disable(String[] ids) {
        return 0;
    }

    @Override
    public int SaveBatch(List<T> entities) throws IllegalArgumentException, IllegalAccessException {
        return 0;
    }
}
